<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户uid" prop="uid">
      <el-input v-model="dataForm.uid" placeholder="用户uid"></el-input>
    </el-form-item>
    <el-form-item label="品牌id" prop="brandId">
      <el-input v-model="dataForm.brandId" placeholder="品牌id"></el-input>
    </el-form-item>
    <el-form-item label="分类id" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="分类id"></el-input>
    </el-form-item>
    <el-form-item label="货号" prop="sku">
      <el-input v-model="dataForm.sku" placeholder="货号"></el-input>
    </el-form-item>
    <el-form-item label="颜色" prop="color">
      <el-input v-model="dataForm.color" placeholder="颜色"></el-input>
    </el-form-item>
    <el-form-item label="尺寸" prop="size">
      <el-input v-model="dataForm.size" placeholder="尺寸"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="文章内容" prop="article">
      <el-input v-model="dataForm.article" placeholder="文章内容"></el-input>
    </el-form-item>
    <el-form-item label="头图" prop="titlePicture">
      <el-input v-model="dataForm.titlePicture" placeholder="头图"></el-input>
    </el-form-item>
    <el-form-item label="详情" prop="descJson">
      <el-input v-model="dataForm.descJson" placeholder="详情"></el-input>
    </el-form-item>
    <el-form-item label="定位" prop="location">
      <el-input v-model="dataForm.location" placeholder="定位"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createdAt">
      <el-input v-model="dataForm.createdAt" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updatedAt">
      <el-input v-model="dataForm.updatedAt" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="删除时间" prop="deletedAt">
      <el-input v-model="dataForm.deletedAt" placeholder="删除时间"></el-input>
    </el-form-item>
    <el-form-item label="审核标志位：0-未审核；1-审核。" prop="checkFlag">
      <el-input v-model="dataForm.checkFlag" placeholder="审核标志位：0-未审核；1-审核。"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          uid: '',
          brandId: '',
          categoryId: '',
          sku: '',
          color: '',
          size: '',
          sex: '',
          article: '',
          titlePicture: '',
          descJson: '',
          location: '',
          createdAt: '',
          updatedAt: '',
          deletedAt: '',
          checkFlag: ''
        },
        dataRule: {
          uid: [
            { required: true, message: '用户uid不能为空', trigger: 'blur' }
          ],
          brandId: [
            { required: true, message: '品牌id不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '分类id不能为空', trigger: 'blur' }
          ],
          sku: [
            { required: true, message: '货号不能为空', trigger: 'blur' }
          ],
          color: [
            { required: true, message: '颜色不能为空', trigger: 'blur' }
          ],
          size: [
            { required: true, message: '尺寸不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          article: [
            { required: true, message: '文章内容不能为空', trigger: 'blur' }
          ],
          titlePicture: [
            { required: true, message: '头图不能为空', trigger: 'blur' }
          ],
          descJson: [
            { required: true, message: '详情不能为空', trigger: 'blur' }
          ],
          location: [
            { required: true, message: '定位不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updatedAt: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          deletedAt: [
            { required: true, message: '删除时间不能为空', trigger: 'blur' }
          ],
          checkFlag: [
            { required: true, message: '审核标志位：0-未审核；1-审核。不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/biz/publishExperts/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.uid = data.publishExperts.uid
                this.dataForm.brandId = data.publishExperts.brandId
                this.dataForm.categoryId = data.publishExperts.categoryId
                this.dataForm.sku = data.publishExperts.sku
                this.dataForm.color = data.publishExperts.color
                this.dataForm.size = data.publishExperts.size
                this.dataForm.sex = data.publishExperts.sex
                this.dataForm.article = data.publishExperts.article
                this.dataForm.titlePicture = data.publishExperts.titlePicture
                this.dataForm.descJson = data.publishExperts.descJson
                this.dataForm.location = data.publishExperts.location
                this.dataForm.createdAt = data.publishExperts.createdAt
                this.dataForm.updatedAt = data.publishExperts.updatedAt
                this.dataForm.deletedAt = data.publishExperts.deletedAt
                this.dataForm.checkFlag = data.publishExperts.checkFlag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/biz/publishExperts/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'uid': this.dataForm.uid,
                'brandId': this.dataForm.brandId,
                'categoryId': this.dataForm.categoryId,
                'sku': this.dataForm.sku,
                'color': this.dataForm.color,
                'size': this.dataForm.size,
                'sex': this.dataForm.sex,
                'article': this.dataForm.article,
                'titlePicture': this.dataForm.titlePicture,
                'descJson': this.dataForm.descJson,
                'location': this.dataForm.location,
                'createdAt': this.dataForm.createdAt,
                'updatedAt': this.dataForm.updatedAt,
                'deletedAt': this.dataForm.deletedAt,
                'checkFlag': this.dataForm.checkFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
